package com.richapp.launcher.ui.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.richapp.launcher.R;
import com.richapp.launcher.ui.fragments.child.camera.CameraControlFragment;
import com.richapp.launcher.ui.fragments.child.curtains.CurtainsControlFragment;
import com.richapp.launcher.ui.fragments.child.electricmonitor.ElectricMonitorControlFragment;
import com.richapp.launcher.ui.fragments.child.light.LightControlFragment;
import com.richapp.launcher.ui.fragments.child.power.PowerControlFragment;
import com.richapp.launcher.ui.fragments.child.scenario.ScenarioControlFragment;

public class BaseChildFragment extends BaseFragment {
	private View baseChild = null;
	private FrameLayout child_light_control, child_curtains_control,
			child_power_control, child_camera_control, child_electric_monitor,
			child_scenario_control;
    private int childTag;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		baseChild = inflater.inflate(R.layout.basechildfragment, container,
				false);
		init(baseChild);
		setChildFragment(childTag);
		return baseChild;
	}
	
	public void setChildTag(int childTag){
		this.childTag = childTag;
	}

	protected void init(View view) {
		child_camera_control = (FrameLayout) view
				.findViewById(R.id.child_camera_control);
		child_curtains_control = (FrameLayout) view
				.findViewById(R.id.child_curtains_control);
		child_electric_monitor = (FrameLayout) view
				.findViewById(R.id.child_electric_monitor);
		child_light_control = (FrameLayout) view
				.findViewById(R.id.child_light_control);
		child_power_control = (FrameLayout) view
				.findViewById(R.id.child_power_control);
		child_scenario_control = (FrameLayout) view
				.findViewById(R.id.child_scenario_control);

		child_camera_control.setOnClickListener(click);
		child_curtains_control.setOnClickListener(click);
		child_electric_monitor.setOnClickListener(click);
		child_light_control.setOnClickListener(click);
		child_power_control.setOnClickListener(click);
		child_scenario_control.setOnClickListener(click);
	}

	OnClickListener click = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.child_camera_control:
				replaceFragment(CameraControlFragment.getInstance(),R.id.base_child);
				break;
			case R.id.child_curtains_control:
				setNextFocusRight(R.id.curtains_child_arrow_up);
				replaceFragment(CurtainsControlFragment.getInstance(),R.id.base_child);
				break;
			case R.id.child_electric_monitor:
				replaceFragment(ElectricMonitorControlFragment.getInstance(),R.id.base_child);
				break;
			case R.id.child_light_control:
				setNextFocusRight(R.id.light_child_arrow_up);
				replaceFragment(LightControlFragment.getInstance(),R.id.base_child);
				break;
			case R.id.child_power_control:
				setNextFocusRight(R.id.power_child_arrow_up);
				replaceFragment(PowerControlFragment.getInstance(),R.id.base_child);
				break;
			case R.id.child_scenario_control:
				setNextFocusRight(R.id.child_roomscenario_gridview);
				replaceFragment(ScenarioControlFragment.getInstance(),R.id.base_child);
				break;
			default:
				break;
			}
		}
	};
	private void setChildFragment(int tag) {
		switch (tag) {
		case BaseFragment.LIGHTCONTROLFLAG:
			child_light_control.requestFocus();
			setNextFocusRight(R.id.light_child_arrow_up);
			replaceFragment(LightControlFragment.getInstance(),R.id.base_child);
		    break;
		case BaseFragment.CAMERACONTROLFLAG:
            child_camera_control.requestFocus();
			break;
		case BaseFragment.CURTAINSCONTROLFLAG:
            child_curtains_control.requestFocus();
            setNextFocusRight(R.id.curtains_child_arrow_up);
            replaceFragment(CurtainsControlFragment.getInstance(),R.id.base_child);
			break;
		case BaseFragment.POWERCONTROLFLAG:
            child_power_control.requestFocus();
            setNextFocusRight(R.id.power_child_arrow_up);
            replaceFragment(PowerControlFragment.getInstance(),R.id.base_child);
			break;
		case BaseFragment.POWERMONITORFLAG:
            child_electric_monitor.requestFocus();
			replaceFragment(ElectricMonitorControlFragment.getInstance(),R.id.base_child);
			break;
		case BaseFragment.SCENARIOCONTROLFLAG:
            child_scenario_control.requestFocus();
            setNextFocusRight(R.id.child_roomscenario_gridview);
			replaceFragment(ScenarioControlFragment.getInstance(),R.id.base_child);
			break;

		default:
			break;
		}
	}
	private void setNextFocusRight(int id){
		child_camera_control.setNextFocusRightId(id);
		child_curtains_control.setNextFocusRightId(id);
		child_electric_monitor.setNextFocusRightId(id);
		child_light_control.setNextFocusRightId(id);
		child_power_control.setNextFocusRightId(id);
		child_scenario_control.setNextFocusRightId(id);
	}
}
